<?php

namespace App\Http\Controllers\UserApi;

use App\Http\Controllers\Controller;
use App\Http\Controllers\UserApi\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UploadController extends BaseController
{
    // 公共上傳類

    /**
     * 上傳圖片
     */
    public function upload_image(Request $request){
        try{
            $rule = ['image' => 'required|mimes:jpg,png,git,jpeg'];
            $message = [
                'image.required' => '请选择需要上传的图片',
                'image.mimes' => '上传的图片仅支持jpg,png,git,jpeg'
            ];
            $validator = Validator::make($request -> all(),$rule,$message);
            if($validator -> fails()){
                throw new \Exception($validator -> errors() -> first());
            }
            $file = $request -> file('image');

            $path = $file -> store('images/' . date('Ymd'));
            //dd($path);

            $data = [
                'data' => [
                    'filename' => config('filesystems.disks.local.url') .$path,
                    'url' => getDomain()  . config('filesystems.disks.local.url') . $path
                ]
            ];
            return $this->success($data);

        }catch (\Exception $e){
            return $this->fail('上传失败-'.$e -> getMessage());
        }
    }
}
